import json from './widget.json'
import Vue from 'vue'
import ala from 'ala'
export default {
	// 检查后台管理员权限
	async checkAdminRole (pageInfo) {
		if (pageInfo.name !== null) {
			await ala.setTitle(pageInfo.name)
		}
		var roleOutput = ala.vuexLocalGet(ala.themeRoleCacheKey(), true)
		if (roleOutput === null) {
			ala.userLogout()
			Vue.prototype.$alert('登录已失效,请重新登录')
			var themeRoute = await ala.themeRoute()
			ala.push(themeRoute.prefix + '/login')
		}
	},
	async setWidget (jsThis) {
		var widget = JSON.parse(JSON.stringify(json))
		widget.path = jsThis.$route.path
		widget.url = jsThis.$route.path
		widget.border = {
			show: false,
			class: ''
		}

		if (jsThis.$route.path.toLowerCase().endsWith('list')) {
			var type = this.getType(jsThis, 'list')
			if (widget.widgets[0].value.table !== undefined) {
				widget.widgets[0].value.table.type = type
			} else {
				var tableArray = {
					type: type
				}
				jsThis.$set(widget.widgets[0].value, 'table', tableArray)
			}
			widget.widgets[0].value.type = type
			widget.widgets[0].componentPath = '/auto/admin-auto-table'
			widget.widgets[0].name = 'admin-auto-table'
			return widget
		} else if (jsThis.$route.path.toLowerCase().endsWith('edit')) {
			type = this.getType(jsThis, 'edit')
			widget.widgets[0].value.type = type
			widget.widgets[0].componentPath = '/auto/admin-auto-form'
			widget.widgets[0].name = 'admin-auto-form'
			return widget
		} else if (jsThis.$route.path.toLowerCase().endsWith('time')) {
			type = this.getType(jsThis, 'time')
			widget.widgets[0].value.type = type
			widget.widgets[0].componentPath = '/auto/admin-auto-time'
			widget.widgets[0].name = 'admin-auto-time'
			return widget
		} else if (jsThis.$route.path.toLowerCase().endsWith('index')) {
			type = this.getType(jsThis, 'index')
			var para = {
				type: type,
				...ala.urlToObject()
			}
			var response = await ala.httpGet('Api/AutoPage/Index', para)
			if (response) {
				widget.widgets = response.widgets
				if (response.name !== null) {
					await ala.setTitle(response.name)
				}
				return widget
			}
		} else if (jsThis.$route.path.toLowerCase().endsWith('preview')) {
			widget.widgets[0].value = {
				type: this.getType(jsThis, 'preview') + 'preview'
			}
			widget.widgets[0].componentPath = '/auto/admin-auto-preview'
			widget.widgets[0].name = 'admin-auto-preview'
			return widget
		} else if (jsThis.$route.path.toLowerCase().endsWith('report')) {
			type = this.getType(jsThis, 'report')
			var para = {
				type: type,
				...ala.urlToObject()
			}
			var response = await ala.httpGet('Api/AutoPage/Report', para)
			if (response) {
				widget.widgets = response.widgets
				if (response.name !== null) {
					await ala.setTitle(response.name)
				}
				return widget
			}
		} else if (jsThis.$route.path.toLowerCase().endsWith('add')) {
			type = this.getType(jsThis, 'add')
			widget.widgets[0].value.type = type
			widget.widgets[0].componentPath = '/auto/admin-auto-form'
			widget.widgets[0].name = 'admin-auto-form'
			return widget
		} else if (jsThis.$route.path.toLowerCase().endsWith('image')) {
			var type = this.getType(jsThis, 'image')
			if (widget.widgets[0].value.table !== undefined) {
				widget.widgets[0].value.table.type = type
			} else {
				var tableArray = {
					type: type
				}
				jsThis.$set(widget.widgets[0].value, 'table', tableArray)
			}
			widget.widgets[0].value = type
			widget.widgets[0].componentPath = '/auto/admin-auto-image'
			widget.widgets[0].name = 'admin-auto-image'
			return widget
		} else if (jsThis.$route.path.toLowerCase().endsWith('video')) {
			var type = this.getType(jsThis, 'video')
			if (widget.widgets[0].value.table !== undefined) {
				widget.widgets[0].value.table.type = type
			} else {
				var tableArray = {
					type: type
				}
				jsThis.$set(widget.widgets[0].value, 'table', tableArray)
			}
			widget.widgets[0].value = type
			widget.widgets[0].componentPath = '/auto/admin-auto-video'
			widget.widgets[0].name = 'admin-auto-video'
			return widget
		}
		return null
	},
	getType (jsThis, type) {
		var setType = jsThis.$route.path.toLowerCase()
		if (jsThis.$route.path.toLowerCase().indexOf(type) !== -1) {
			setType = setType.replace('admin-' + jsThis.type, '')
			setType = setType.replace('admin/data/', '')
			setType = setType.replace('admin/', '')
			setType = setType.replace(type, '')
			setType = setType.replace('//', '')
			setType = setType.replace('/', '')
			setType = setType.replace('/', '')
			setType = setType.replace('/', '')
		}
		if (jsThis.type.toLowerCase() !== 'admin') {
			setType = jsThis.type + setType
		}
		return setType
	}
}